<template>
	<div style="height:100%;">
		<div style="margin-bottom: 10px;overflow: hidden;">
			<input type="file" name="file" id="plFile" @change="upload" style="visibility: hidden;width: 0;height: 0;" multiple>
			<el-button size="mini" type="primary" style="float:right" @click="uploadClick" v-if="['查看模板','模板审核','任务详情','任务签发'].indexOf(title)<0 && $parent.$parent.userOpernation.upload.display">上传附件</el-button>
		</div>
		<el-table :data="renwufujian" style="width: 100%;height:calc(100% - 20px)" class="tableCon" size="mini" highlight-current-row cell-class-name="batchtablecell"
		 @current-change="taskTableRowSelect" key="renwufujian">
			<el-table-column label="序号" width="49px" fixed>
				<template slot-scope="scopea">
					{{scopea.$index+1}}
				</template>
			</el-table-column>
			<el-table-column prop="fileName" label="名称" min-width="90px">
			</el-table-column>
			<el-table-column label="上传人" prop="createByName" min-width="90px">
			</el-table-column>
			<el-table-column label="操作" min-width="90px">
				<template slot-scope="scope">
					<el-button type="text" size="small" @click="deleteattachment(scope.row)" v-if="['查看模板','模板审核','任务详情','任务签发'].indexOf(title)<0 && $parent.$parent.userOpernation.delete.display">删除</el-button>
					<el-button type="text" size="small" @click="dlownloadattachment(scope.row)" v-if = "$parent.$parent.userOpernation.download.display">下载</el-button>
				</template>
			</el-table-column>
		</el-table>
	</div>
</template>

<script>
	export default {
		props: ['title', 'getPlanData'],
		data() {
			return {
				renwufujian: [],
			}
		},
		mounted() {
			this.init();
			// console.log(this.$parent.$parent.userOpernation)

		},
		methods: {

			init() {
				this.$http({
					method: 'post',
					apiUrl: 'download/getFilePathByIds.htm',
					params: { ids: this.getPlanData.id },
					isJson: false,
				}).then(res => {
					if (res.code == 0) {
						this.renwufujian = res.data
					} else {

					}

				}).catch(() => {

				});
			},
			uploadClick() {
				document.getElementById('plFile').click()
			},
			upload(event) {
				var files = document.getElementById('plFile').files;

				var formData = new FormData();
				for(var i of files){
					formData.append('file', i)
				}
				var fileType;
				if (this.title == '编辑模板') {
					fileType = 'plan'
				} else {
					fileType = 'task'
				}
				// formData.append('file', files)
				formData.append('ids', this.getPlanData.id)
				formData.append('type', 3)
				formData.append('taskType', null)
				formData.append('detectability', null)
				formData.append('fileType', fileType)
				this.$axios({
					method: 'post',
					url: '/download/fileUpload.htm',
					data: formData,
					headers: { 
            'Content-Type': 'multipart/form-data',
            'token':sessionStorage.getItem('loginAccount')?sessionStorage.getItem('loginAccount'):undefined
          }
				}).then(res => {
					event.target.value= "";
					var data = res.data
					if (data.code == 0) {
						this.$notify({
							title: '消息',
							message: '上传成功',
							type: 'success'
						})
						this.init();
					} else {
						this.$message({
							type: 'info',
							message: data.description,
						})
					}

				})
			},
			deleteattachment(row) {
				this.$confirm('是否删除', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$http({
						method: 'post',
						apiUrl: '/download/fileDelete.htm',
						params: {
							id: row.id,
						},
						isJson: false,
					}).then(res => {
						if (res.code === 0) {
							this.$message({
								type: 'success',
								message: '删除成功!'
							});
							this.init()
						}
					})
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
			// 下载附件
			dlownloadattachment(row) {
				var url = "/download/fileDownload.htm"
				// if(this.taskListValue == ''){

				// }else{
          let token = sessionStorage.getItem('loginAccount')?sessionStorage.getItem('loginAccount'):undefined
				window.open(process.env.baseUrl + url + '?id=' + row.id + "&token=" + token);
				// }
			},
			taskTableRowSelect() {
				console.log(1)
			}
		}
	}

</script>

<style>

</style>